package com.example.homeworkdev.pages;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.homeworkdev.R;
import com.example.homeworkdev.fragment.ContentFragment;
import com.example.homeworkdev.fragment.FindFragment;
import com.example.homeworkdev.fragment.MessageFragment;
import com.example.homeworkdev.entity.Message;
import com.example.homeworkdev.fragment.MyInfoFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    RelativeLayout shouye;
    RelativeLayout faxian;
    RelativeLayout xiaoxi;
    RelativeLayout wode;

    ContentFragment contentFragment = new ContentFragment();
    FindFragment findFragment = new FindFragment();
    MessageFragment messageFragment = new MessageFragment();
    MyInfoFragment myInfoFragment = new MyInfoFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);

        //内容
        replaceFragment(R.id.fragment_content,contentFragment);

        initData();
    }

    //    初始化数据
    private void initData(){
        shouye = findViewById(R.id.bottom_shouye);
        faxian = findViewById(R.id.bottom_faxian);
        xiaoxi = findViewById(R.id.bottom_xiaoxi);
        wode = findViewById(R.id.bottom_wode);
        shouye.setOnClickListener(this);
        faxian.setOnClickListener(this);
        xiaoxi.setOnClickListener(this);
        wode.setOnClickListener(this);
    }

    //更换加载fragment
    private void replaceFragment(int fragmentId, Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(fragmentId,fragment);
        fragmentTransaction.commit();
    }



    //    点击选中项事件
    @Override
    public void onClick(View v) {
        int clickTabId = v.getId();
        tabChange(v);
        if(clickTabId == shouye.getId()){
            replaceFragment(R.id.fragment_content,contentFragment);
        } else if (clickTabId == faxian.getId()) {
            replaceFragment(R.id.fragment_content,findFragment);
        } else if (clickTabId == xiaoxi.getId()) {
            replaceFragment(R.id.fragment_content,messageFragment);
        } else if (clickTabId == wode.getId()) {
            replaceFragment(R.id.fragment_content,myInfoFragment);
        }

    }

    //    变更选中样式
    private void tabChange(View v) {
        ImageView shouyeImage = findViewById(R.id.shouye);
        ImageView faxianImage = findViewById(R.id.faxian);
        ImageView xiaoxiImage = findViewById(R.id.xiaoxi);
        ImageView wodeImage = findViewById(R.id.wode);

        TextView shouyeText = findViewById(R.id.shouye_text);
        TextView faxianText = findViewById(R.id.faxian_text);
        TextView xiaoxiText = findViewById(R.id.xiaoxi_text);
        TextView wodeText = findViewById(R.id.wode_text);

        if(v.getId() == shouye.getId()){
            shouyeImage.setImageResource(R.drawable.select_shouye);
            faxianImage.setImageResource(R.drawable.faxian);
            xiaoxiImage.setImageResource(R.drawable.xiaoxi);
            wodeImage.setImageResource(R.drawable.wode);

            shouyeText.setTextColor(getColor(R.color.select_bottom));
            shouyeText.setTypeface(null, Typeface.BOLD);
            faxianText.setTextColor(getColor(R.color.black));
            faxianText.setTypeface(null,Typeface.NORMAL);
            xiaoxiText.setTextColor(getColor(R.color.black));
            xiaoxiText.setTypeface(null,Typeface.NORMAL);
            wodeText.setTextColor(getColor(R.color.black));
            wodeText.setTypeface(null,Typeface.NORMAL);

        }else if (v.getId() == faxian.getId()){
            shouyeImage.setImageResource(R.drawable.zhuye);
            faxianImage.setImageResource(R.drawable.select_faxian);
            xiaoxiImage.setImageResource(R.drawable.xiaoxi);
            wodeImage.setImageResource(R.drawable.wode);

            shouyeText.setTextColor(getColor(R.color.black));
            shouyeText.setTypeface(null, Typeface.NORMAL);
            faxianText.setTextColor(getColor(R.color.select_bottom));
            faxianText.setTypeface(null,Typeface.BOLD);
            xiaoxiText.setTextColor(getColor(R.color.black));
            xiaoxiText.setTypeface(null,Typeface.NORMAL);
            wodeText.setTextColor(getColor(R.color.black));
            wodeText.setTypeface(null,Typeface.NORMAL);
        } else if (v.getId() == xiaoxi.getId()) {
            shouyeImage.setImageResource(R.drawable.zhuye);
            faxianImage.setImageResource(R.drawable.faxian);
            xiaoxiImage.setImageResource(R.drawable.select_xiaoxi);
            wodeImage.setImageResource(R.drawable.wode);

            shouyeText.setTextColor(getColor(R.color.black));
            shouyeText.setTypeface(null, Typeface.NORMAL);
            faxianText.setTextColor(getColor(R.color.black));
            faxianText.setTypeface(null,Typeface.NORMAL);
            xiaoxiText.setTextColor(getColor(R.color.select_bottom));
            xiaoxiText.setTypeface(null,Typeface.BOLD);
            wodeText.setTextColor(getColor(R.color.black));
            wodeText.setTypeface(null,Typeface.NORMAL);
        }else if(v.getId() == wode.getId()){
            shouyeImage.setImageResource(R.drawable.zhuye);
            faxianImage.setImageResource(R.drawable.faxian);
            xiaoxiImage.setImageResource(R.drawable.xiaoxi);
            wodeImage.setImageResource(R.drawable.select_wode);

            shouyeText.setTextColor(getColor(R.color.black));
            shouyeText.setTypeface(null, Typeface.NORMAL);
            faxianText.setTextColor(getColor(R.color.black));
            faxianText.setTypeface(null,Typeface.NORMAL);
            xiaoxiText.setTextColor(getColor(R.color.black));
            xiaoxiText.setTypeface(null,Typeface.NORMAL);
            wodeText.setTextColor(getColor(R.color.select_bottom));
            wodeText.setTypeface(null,Typeface.BOLD);
        }

    }


}